package com.bhs.sansonglogistics.ui.order;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.os.Bundle;
import android.text.TextUtils;
import android.util.ArrayMap;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.RelativeLayout;
import android.widget.Spinner;
import android.widget.TextView;

import com.bhs.sansonglogistics.R;
import com.bhs.sansonglogistics.base.BaseActivity;
import com.bhs.sansonglogistics.base.BaseBean;
import com.bhs.sansonglogistics.bean.PictureBean;
import com.bhs.sansonglogistics.bean.event.RefreshOrder;
import com.bhs.sansonglogistics.dialog.FeeTypeDialog;
import com.bhs.sansonglogistics.dialog.MyClickListener2;
import com.bhs.sansonglogistics.net.NetCallBack;
import com.bhs.sansonglogistics.ui.adapter.PictureSelectAdapter;
import com.bhs.sansonglogistics.utils.FastCilckOneUtil;
import com.bhs.sansonglogistics.utils.ToastUtil;
import com.google.gson.Gson;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import okhttp3.ResponseBody;
import retrofit2.Response;

/**
 * 增加费用
 */
public class IncrementalExpensesActivity extends BaseActivity implements View.OnClickListener, NetCallBack {


    private EditText mEtCost;
    private RecyclerView mRvList;

    private PictureSelectAdapter adapter;
    private TextView mTvFeeType;
    private String type;
    @Override
    public int initLayout() {
        return R.layout.activity_incremental_expenses;
    }

    @Override
    public void initView() {
        type=getIntent().getStringExtra("type");
        mEtCost = findViewById(R.id.et_cost);
        mRvList = findViewById(R.id.rv_list);
        findViewById(R.id.btn_submit).setOnClickListener(this);
        TextView mTvTitle = findViewById(R.id.tv_title);
        mTvTitle.setText("增加费用");
        mTvFeeType = findViewById(R.id.tv_fee_type);
        mTvFeeType.setOnClickListener(this);
    }

    @Override
    public void initData() {
        adapter = new PictureSelectAdapter(3, this);
        mRvList.setAdapter(adapter);
        mRvList.setLayoutManager(new GridLayoutManager(this, 3));
    }

    FeeTypeDialog feeTypeDialog;

    @Override
    public void onClick(View view) {
        if (FastCilckOneUtil.isFastClick()) {
            return;
        }
        if (view.getId() == R.id.btn_submit) {
            submit();
        } else {
            if (feeTypeDialog == null) {
                feeTypeDialog = new FeeTypeDialog(mActivity);
                feeTypeDialog.setMyClickListener(new MyClickListener2() {
                    @Override
                    public void onClick(String id, String info) {
                        mTvFeeType.setText(info);
                    }
                });
            }
            feeTypeDialog.show();
        }
    }

    private void submit() {
        if (TextUtils.isEmpty(mTvFeeType.getText())) {
            ToastUtil.show("请选择费用类型");
            return;
        }
        if (TextUtils.isEmpty(mEtCost.getText())) {
            ToastUtil.show("请输入费用");
            return;
        }
        Map<String, String> map = new ArrayMap<>();
        map.put("deliver_sn", getIntent().getStringExtra("deliver_sn"));
        map.put("money", mEtCost.getText().toString());
        map.put("fee_notice", mTvFeeType.getText().toString());
        map.put("fee_pic", adapter.getImages());
        networkRequest(netApi.add_order_fee(map), this);
    }

    @Override
    public void onResponse(int requestCode, String json, Response<ResponseBody> response) throws Exception {
        BaseBean bean = new Gson().fromJson(json, BaseBean.class);
        ToastUtil.show(bean.getMsg());
        if (bean.isStatus()) {
            finish();
        }
    }

    @Override
    public void onFailure(int requestCode) {

    }
}